﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using System.Data.Entity;
using BLL;
namespace CMS项目.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        [HttpGet]
        //登录
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public int Login(string Uname, string upwd)
        {
            using (var db = new KQCMS())
            {
                if (string.IsNullOrEmpty(Uname) || string.IsNullOrEmpty(upwd))
                {
                    Response.Write("<script>alert('用户名或密码不能为空')</script>");
                }
                int count = db.CMS_User.Where(u => u.Uname.Equals(Uname) && u.upwd.Equals(upwd)).Count();
                Session["User"] = db.CMS_User.Where(u => u.Uname.Equals(Uname) && u.upwd.Equals(upwd)).ToList();
                return count;

            }

        }

        //注册
        [HttpGet]
        public ActionResult register()
        {
            return View();
        }
        [HttpPost]
        public int register(CMS_User user)
        {
            using (var db = new KQCMS())
            {

                db.Entry(user).State = EntityState.Added;
                return db.SaveChanges();

            }

        }

        //主页
        [HttpGet]
        public ActionResult Index()
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_Category.Take(4).ToList();
                ViewBag.Category = list;//栏目
                ViewBag.Article = db.CMS_Article.Where(x => x.cid == 1).OrderByDescending(x => x.hits).ToList();//文章 网站公告
                ViewBag.Article2 = db.CMS_Article.Where(x => x.cid == 2).OrderByDescending(x => x.hits).ToList();//文章  产品中心
                ViewBag.Article3 = db.CMS_Article.Where(x => x.cid == 3).OrderByDescending(x => x.hits).ToList();//文章  定制服务
                ViewBag.Article4 = db.CMS_Article.Where(x => x.cid == 4).OrderByDescending(x => x.hits).ToList();//文章  成功案例
                return View(BLL_CMS_Keyword.SelectKeywords());
            }
        }
        //栏目
        public ActionResult CPZX()
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_Category.Take(4).ToList();
                ViewBag.Category = list;//栏目
                return View(BLL_CMS_Keyword.SelectKeywords());//关键字
            }

        }
        [HttpGet]
        public ActionResult CPZX(int? id)
        {
            using (var db = new KQCMS())
            {
                ViewBag.Category2 = db.CMS_Article.Where(x => x.cid == id).OrderByDescending(x => x.hits).ToList();
                var list = db.CMS_Category.Take(4).ToList();
                ViewBag.Category = list;//栏目
                return View(BLL_CMS_Keyword.SelectKeywords());//关键字
            }

        }
        public ActionResult CPZX2()
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_Category.Take(4).ToList();
                ViewBag.Category = list;//栏目
                return View(BLL_CMS_Keyword.SelectKeywords());//关键字
            }

        }
        [HttpGet]
        public ActionResult CPZX2(string name)
        {
            using (var db = new KQCMS())
            {
                ViewBag.Category2 = db.CMS_Article.Where(x => x.title.Contains(name) || x.ahtml.Contains(name)).OrderByDescending(x => x.hits).ToList();
                var list = db.CMS_Category.Take(4).ToList();
                ViewBag.Category = list;//栏目
                return View(BLL_CMS_Keyword.SelectKeywords());//关键字
            }
        }
        [HttpPost]
        public ActionResult CPZX2(string name, int a)
        {
            using (var db = new KQCMS())
            {
                ViewBag.Category2 = db.CMS_Article.Where(x => x.title.Contains(name) || x.ahtml.Contains(name)).OrderByDescending(x => x.hits).ToList();
                var list = db.CMS_Category.Take(4).ToList();
                ViewBag.Category = list;//栏目
                return JavaScript("window.location.href='https://localhost:44347/Home/CPZX2?name=" + name + "';");
            }
        }
        //文章详情
        public ActionResult CPZX3()
        {
            using (var db = new KQCMS())
            {
                var list = db.CMS_Category.Take(4).ToList();
                ViewBag.Category = list;//栏目
                return View(BLL_CMS_Keyword.SelectKeywords());//关键字
            }
        }
        [HttpGet]
        public ActionResult CPZX3(int id)
        {
            using (var db = new KQCMS())
            {
                ViewBag.Action = db.View_Article.Where(x => x.aid == id).ToList();//ID查询 文章
                ViewBag.Comment = db.View_CommentAU.Where(x => x.aid == id).ToList();  //ID查询 评论
                ViewBag.count = db.View_CommentAU.Where(x => x.aid == id).Count();
                var list = db.CMS_Category.Take(4).ToList();
                ViewBag.Category = list;//栏目
                return View(BLL_CMS_Keyword.SelectKeywords());//关键字
            }
        }

    }
}